package com.qs.user.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.qs.base.base.BaseMyFragment;
import com.qs.base.router.RouterFragmentPath;
import com.qs.base.utils.FileUtil;
import com.qs.user.BR;
import com.qs.user.R;
import com.qs.user.databinding.UserMyFileFragmentBinding;
import com.qs.user.ui.VM.MyFileItemViewModel;
import com.qs.user.ui.VM.MyFileViewModel;

import java.io.File;

import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * 我的文件fragment
 *
 * @Author ltzz
 * @Date 2019/8/24
 */
@Route(path = RouterFragmentPath.User.MY_FILE_FRAGMENT)
public class MyFileFragment extends BaseMyFragment<UserMyFileFragmentBinding, MyFileViewModel> {

    @Autowired()
    int type;

    @Override
    protected View titleLayout() {
        return null;
    }

    @Override
    protected boolean statusBarDarkFont() {
        return false;
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.user_my_file_fragment;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        viewModel.type = type;
        viewModel.myFiles = FileUtil.getHistorTDFile(FileUtil.bPath + "/out/", type);
        viewModel.itemx.clear();
        viewModel.observableList.clear();
        if (viewModel.myFiles.size() > 0) {
            for (int i = 0; i < viewModel.myFiles.size(); i++) {
                viewModel.observableList.add(new MyFileItemViewModel(viewModel, viewModel.myFiles.get(i), View.GONE));
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    //多选按钮点击
    public void moreSelectClick(boolean isSelect) {
        viewModel.itemx.clear();
        viewModel.observableList.clear();
        int isSelectInt;
        if (isSelect)
            isSelectInt = View.VISIBLE;
        else
            isSelectInt = View.GONE;
        for (int i = 0; i < viewModel.myFiles.size(); i++) {
            viewModel.observableList.add(new MyFileItemViewModel(viewModel, viewModel.myFiles.get(i), isSelectInt));
        }
    }

    public void moreSelectUc2(boolean is){
        KLog.d("sdafsrewrw");
    }


    //删除文件
    public void delete() {
        if (viewModel.itemx.size() > 0) {
            for (MyFileItemViewModel item : viewModel.itemx) {
                //删除文件
                boolean s = FileUtil.deleteFile(FileUtil.bPath + "/out/" + item.mItemEntity.get().getFileName());
                if (s) {
                    ToastUtils.showShort("删除成功");
                    viewModel.observableList.remove(item);
                } else
                    ToastUtils.showShort("删除失败");
            }
        } else
            ToastUtils.showShort("请选择要删除的文件");
    }

    public void toIntent(File parentFlie){
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setDataAndType(Uri.fromFile(parentFlie), "*/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivity(intent);
    }

}
